<template>

  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="标题">
        <el-input v-model="banner.title"/>
      </el-form-item>
      <el-form-item label="链接地址">
      <el-input v-model="banner.linkUrl"/>
    </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="banner.sort" controls-position="right" :min="0"/>
      </el-form-item>
      <!-- 课程封面-->
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_API+'/eduoss/file/upload'"
          class="avatar-uploader">
          <img :src="banner.imageUrl">
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import banner from '@/api/edu/banner'
  import ImageCropper from '@/components/ImageCropper'
  import PanThumb from '@/components/PanThumb'

  export default {
    components: { ImageCropper, PanThumb },
    data() {
      return {
        banner: {
          title: '',
          sort: 0,
          linkUrl: '',
          imageUrl: 'https://online-teach-file.oss-cn-beijing.aliyuncs.com/cms/2019/11/13/8f80790d-d736-4842-a6a4-4dcb0d684d4e.jpg'
        },
        BASE_API: process.env.BASE_API,// 接口API地址
        saveBtnDisabled: false, // 保存按钮是否禁用,
      }
    },
    methods: {
      // 初始化
      init() {
        // 获取路由中的参数
        if (this.$route.params && this.$route.params.id) {
          const id = this.$route.params.id
          this.getBannerById(id)
        }
      },
      // 根据id查询老师
      getBannerById(id) {
        banner.getBannerInfo(id).then(response => {
          this.banner = response.data.item
        })
      },
      // 保存或者修改
      saveOrUpdate() {
        if (this.banner.id) {
          this.updateBanner()
        } else {
          this.saveBanner()
        }
      },
      // 修改
      updateBanner() {
        banner.updateBannerInfo(this.banner).then(response => {
          // 提示信息
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          // 回到列表页面，路由跳转
          this.$router.push({ path: '/edu/banner/table' })
        })
      },
      // 保存
      saveBanner() {
        banner.addBannerInfo(this.banner).then(response => {
          // 提示信息
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          // 回到列表页面，路由跳转
          this.$router.push({ path: '/edu/banner/table' })
        })
      },
      // 上传之前调用的方法
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      },
      // 上传成功之后调用的方法
      handleAvatarSuccess(res, file) {
        console.log(res)// 上传响应
        console.log(URL.createObjectURL(file.raw))// base64编码
        this.banner.imageUrl = res.data.url
      },
    },
    watch: {
      // 监听路由变化
      $route(to, from) {
        // console.log('watch $route')
        this.init()
      }
    },
    created() {
      // console.log('created')
      this.init()
    }
  }
</script>
